var app = getApp()
import * as appConfig from "../../../app-config.js";

Page({

  data: {
    accountList: [],
    agentId:'',
    accountId:'',
    needAdd:true
  },

  
  onLoad: function(options) {
    this.setData({
      accountList:[]
    })
    app.getMemInfo().then(res=>{
      let agentId = res.data.agentId
      let accountId = res.data.id
      let role = res.data.role
      if (role=='BOSS') {
        this.setData({
          needAdd:true
        })
      }else{
        this.setData({
          needAdd:false
        })
      }
      this.setData({
        agentId,
        accountId
      })
      this.accountLists();
    })
  },
  onShow(){
    app.getMemInfo().then(res=>{
      let agentId = res.data.agentId
      let accountId = res.data.id
      let role = res.data.role
      if (role=='BOSS') {
        this.setData({
          needAdd:true
        })
      }else{
        this.setData({
          needAdd:false
        })
      }
      this.setData({
        agentId,
        accountId
      })
      this.accountLists();
    })
  },
  accountLists: function() {
    var that = this;
    var url = "/wx_app/agent/account/list";
    var data = {
      agentId:this.data.agentId,
      accountId:this.data.accountId
    }
    console.log(data)
    app.request(url, data, 'GET').then(res => {
      console.log(res)
      this.accountList(res);
    })
  },

  //门店账号信息转化
  accountList: function (res) {
    var list = res.data;
    var accountList=[];
    list.forEach(item => {
      accountList.push({
        id: item.id,
        name: item.name,
        number: item.phone,
        password: item.password,
        role:item.role
      });
    })
    
    this.setData({
      accountList: accountList
    })

  },


  bindAdd() {
    wx.navigateTo({
      url: 'addAccount'
    })
  },
  bindEdit(e) {
    let name = e.currentTarget.dataset.name
    let num = e.currentTarget.dataset.num
    let password = e.currentTarget.dataset.pas
    let id = e.currentTarget.dataset.id
    console.log(name)
    wx.navigateTo({
      url: 'editAccount?name=' + name + '&num=' + num + '&password=' + password + '&id=' + id
    })
  }



})